<template>
  <div class="line-chart-wrapper">
    <div class="markpoint-tips">
      <span><i class="dot red"></i>买入</span>
      <span><i class="dot green"></i>卖出</span>
    </div>
    <div class="line-chart" ref="line-chart">
    </div>
  </div>
</template>

<script>
import echarts from "echarts/lib/echarts";

import "echarts/lib/chart/line";
import "echarts/lib/component/legend";
import "echarts/lib/component/markPoint"

export default {
  mounted() {
    const chart = echarts.init(this.$refs['line-chart']);
    chart.setOption({
      color: ['#4C7AFF', '#FEC002 '],
      legend: {
        top: '5%',
        left: '5%',
        itemHeight: 3,
        itemWidth: 8,
        data: [
          {
            name: '组合',
            icon: 'rect',
            textStyle: {
              color: '#666',
              fontSize: 12
            }
          },
          {
            name: '沪深300',
            icon: 'rect',
            textStyle: {
              color: '#666',
              fontSize: 12
            }
          }
        ]
      },
      grid: {
        top: '50',
        bottom: '30',
        left: '55',
        right: '10'
      },
      xAxis: {
        data: [],
        axisLine: {
          show: true,
          onZero: false,
          lineStyle: {
            color: '#eee',
            width: 1
          }
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          textStyle: {
            color: '#666'
          }
        }
      },
      yAxis: {
        axisTick: {
          show: false
        },
        showMinLabel: true,
        splitNumber: 3,
        position: 'left',
        axisLine: {
          show: false
        },
        lineHeight: 56,
        axisLabel: {
          inside: false,
          verticalAlign: 'bottom',
          margin: 0,
          textStyle: {
            color: '#bbb'
          },
          formatter: function(value, index) {
            if (index === 0) {
              return '';
            }
            return value === 0 ? value : value.toFixed(2) + '%';
          }
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: '#e7e7e7',
            width: 1
          }
        }
      },
      series: [
        {
          data: [],
          name: '组合',
          type: 'line',
          smooth: true,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1.5,
            }
          },
          markLine: {
            lineStyle: {
              normal: {
                width: 10
              }
            }
          },
          markPoint: {
            data: [],
          }
        },
        {
          data: [],
          name: '沪深300',
          type: 'line',
          smooth: true,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1.5,
            }
          }
        }
      ],
      backgroundColor: '#FFF'
    });
    this.chart = chart;
  },
  methods: {
    showLoading() {
      this.chart.showLoading();
    },
    hideLoading() {
      this.chart.hideLoading();
    },
    setOption(options) {
      console.log(options)
      this.chart.setOption(options);
    }
  }
};
</script>

<style lang="less">
.line-chart-wrapper {
  position: relative;
  margin-top: 15px;

  .markpoint-tips {
    position: absolute;
    right: 10px;
    top: 14px;
    z-index: 2;
    color: #999;
    font-size: 12px;

    span:not(:last-child) {
      margin-right: 15px;
    }

    .dot {
      border-radius: 50%;
      width: 5px;
      height: 5px;
      display: inline-block;
      vertical-align: middle;
      margin-right: 3px;

      &.red {
        background-color: #f8373f;
      }

      &.green {
        background-color: #1db44a;
      }

      &.blue {
        background-color: #8475d8;
      }
    }
  }

  .line-chart {
    height: 180px;
  }
}
</style>
